//有一次抽奖机会
var awardTime = true;
var exist = true;
var sMobile = null;
var arrEle = [];
var arr = ['main_1', 'main_2', 'main_3', 'main_5', 'main_8', 'main_7', 'main_6', 'main_4'];
//元素在arrEle数组中
for(var i = 0; i < arr.length; i++) {
	arrEle.push($(arr[i]));
}
var num = null;
//固定转5次
var cishu = 0;

//验证登陆
function checkMobile() {
	localStorage.setItem('1', 'true');
	sMobile = document.mobileform.email.value
	console.log(sMobile);
	if(!(/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(sMobile))) {
		$('login_hint').classList.remove('hide');
		document.mobileform.email.focus();
		return false;
	} else {
		for(var i = 0; i < localStorage.length; i++) {
			var key = localStorage.key(i);
			var value = localStorage.getItem(key);
			if(sMobile == key) {
				exist = true;
				break;
			} else {
				exist = false;
			}
		}

		//已经注册过用户名了
		if(exist) {
			awardTime = !value ? 1 : 0;
			$('box_change').innerText = +awardTime;
			if(awardTime) {
				$('main_center').onclick = turn;
			} else {
				$('box_change').innerText = +awardTime;
				$('main_center').onclick = null;
			}
		} else {
			$('main_center').onclick = turn;
			localStorage.setItem(sMobile, awardTime);
		}
		$('login_hint').classList.add('hide')
		hideShow('login');
		hideShow('box');
	}
	return false;
}

var turn = function() {
	num = Math.ceil(Math.random() * 8);
	again(cishu);
}
//固定转完5圈之后,再转到随机的li
var start = function(n) {
	n <= 5 ? again(n) : end(num);
}

var again = function(n) {

	for(var i = 0; i < 9; i++) {
		setTimeout(function(i) {
			return function() {
				i > 0 ? arrEle[i - 1].classList.remove('active') : arrEle[i].classList.remove('active');
				i == 8 ? start(++n) : arrEle[i].classList.add('active');
			}
		}(i), i * 50)
	}

}

var end = function(num) {

	for(var i = 0; i < num; i++) {
		setTimeout(function(i) {
			return function() {
				i > 0 ? arrEle[i - 1].classList.remove('active') : arrEle[i].classList.remove('active');
				arrEle[i].classList.add('active');
			}
		}(i), i * 100)
	}

	$('box_change').innerText = 0;
	$('main_center').onclick = null;
	awardTime = false;
	localStorage.setItem(sMobile, awardTime);
}

//获取元素
function $(obj) {
	return document.getElementById(obj)
}

//显示隐藏
function hideShow(id) {
	document.getElementById(id).classList.toggle('hide');
}